<template>
	<view class="container">
		<view class="content">
			<header-view></header-view>
			<view class="search flex_center">
				<uni-icons type="search" size="20" @tap="toSearch"></uni-icons>
			</view>
			<view class="title">行情</view>
			<view class="nav flex">
				<view v-for="item in categories" :key="item.id" class="nav_item flex_center"
					:class="current===item.id?'active_nav':''" @tap="changeNav(item.id)">
					{{ item.name }}
				</view>
				<view class="nav_bg" :class="getNavBgClass(current)"></view>
			</view>

			<view class="list">
				<view class="price_item" v-for="item in list" @tap="toDetail(item.id)">
					<view class="item_header">
						<view class="symbol font_title">{{item.name}}</view>
						<view class="time">
							{{ getCurrentTime() }}
						</view>
					</view>
					<view class="price">
						<view class="price_new" :class="item.rate>0?'up':'down'">
							{{ item.sell_price.toString().slice(0, 9) }}
						</view>
						<view class="price_old">{{item.low}}</view>
					</view>
					<view class="price">
						<view class="price_new" :class="item.rate>0?'up':'down'">
							{{ item.buy_price.toString().slice(0, 9) }}
						</view>
						<view class="price_old">{{item.high}}</view>
					</view>
					<view class="detail" :class="item.rate>0?'bg_up':'bg_down'">{{item.rate}}%</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import headerView from "@/components/headerView.vue"
	import {
		cateListApi,
		stockListApi
	} from '@/api/api.js'
	export default {
		components: {
			headerView
		},
		data() {
			return {
				current: 1,
				list: [],
				categories: [],
				page: 1,
				timer: null,
			};
		},
		onShow() {
			this.getCateList()
			this.getStockList()
			this.timer = setInterval(() => {
				this.getStockList()
			}, 3000 * 10)
		},
		onHide() {
			clearInterval(this.timer)
		},
		onUnload() {
			clearInterval(this.timer)
		},
		beforeDestroy() {
			clearInterval(this.timer)
		},
		onReachBottom() {
			console.log("滚动到底部")
			this.page++
			this.getStockList()
		},
		methods: {
			toSearch() {
				uni.navigateTo({
					url: "/pages/home/search"
				})
			},
			getCurrentTime() {
				const currentDate = new Date();
				const hours = currentDate.getHours().toString().padStart(2, '0'); // 将小时转为字符串，并在不足两位时前面补0
				const minutes = currentDate.getMinutes().toString().padStart(2, '0'); // 同上
				const seconds = currentDate.getSeconds().toString().padStart(2, '0'); // 同上
				return `${hours}:${minutes}:${seconds}`;
			},
			// 获取产品列表
			getStockList() {
				if (this.total == this.list.length) {
					this.page--
					uni.$u.toast("已经到底啦~")
					return
				}
				const cate_id = this.current;
				stockListApi({
					page: this.page,
					cate_id,
				}).then(res => {
					// 处理接口返回的结果
					this.total = res.data.data.total
					if (this.page == 1) {
						this.list = res.data.data.data
					} else {
						this.list = [...this.list, ...res.data.data.data]
					}
				})
			},
			// 获取分类
			getCateList() {
				const token = uni.getStorageSync('token')
				cateListApi({
					token
				}).then(res => {
					// 处理接口返回的结果
					this.categories = res.data.data
				})
			},
			changeNav(key) {
				if (key == this.current) return
				this.current = key
				this.list = []
				this.page = 1
				this.getStockList()
			},
			getNavBgClass(id) {
				const classes = {
					'nav_left': id === 1,
					'nav_right': id === 2,
				};
				return classes;
			},
			toDetail(opt) {
				const id = opt
				uni.navigateTo({
					url: "/pages/detail/index?id=" + id
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		min-height: 100vh;
		height: auto;

		.content {
			padding-bottom: 40rpx;
			position: relative;
			// padding-right: 0;

			.title {
				padding-top: 60rpx;
				font-size: 30px;
				font-weight: 500;
				font-family: PM;
				line-height: 92rpx;
			}

			.search {
				position: absolute;
				right: 40rpx;
				top: calc(40rpx + var(--status-bar-height));
				width: 72rpx;
				height: 72rpx;
				background-color: hsla(0, 0%, 92.9%, .46);
				border-radius: 20rpx;
			}

			.nav {
				height: 100rpx;
				line-height: 100rpx;
				background-color: rgba(237, 237, 237, .46);
				border-radius: 40rpx;
				overflow: hidden;
				position: relative;
				font-size: 28rpx;
				margin: 40rpx 0 60rpx;

				.nav_item {
					flex: 1;
					position: relative;
					z-index: 10;
					opacity: .3;
					transition: all .3s ease;
				}

				.nav_bg {
					position: absolute;
					top: 6rpx;
					background-image: url("../../static/img/page_bg.png");
					background-size: cover;
					z-index: 1;
					border-radius: 40rpx;
					width: calc(50% - 6rpx);
					height: calc(100% - 12rpx);
					z-index: 9;
					transition: all .3s ease;
				}

				.nav_left {
					left: 6rpx;
				}

				.nav_right {
					left: 50%;
				}

				.active_nav {
					color: #0065ff;
					opacity: 1;
				}
			}

			.list {
				.price_item {
					width: 100%;
					color: #303133;
					display: grid;
					grid-template-columns: auto 130rpx 130rpx 130rpx;
					align-items: center;
					margin-bottom: 40rpx;
					grid-column-gap: 30rpx;

					.item_header {
						.symbol {
							font-size: 28rpx;
						}

						.time {
							font-size: 20rpx;
							opacity: .3;
						}
					}

					.price {
						font-size: 28rpx;
						text-align: center;

						.price_old {
							font-size: 20rpx;
							opacity: .3;
						}
					}

					.detail {
						height: 50rpx;
						line-height: 50rpx;
						font-size: 24rpx;
						border-radius: 10rpx;
						text-align: center;
					}
				}
			}
		}
	}
</style>